package com.css.fxfzypg.base.exception;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Dbj
 * @date 2022年09月06日  11:26
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 新增或修改是违反了完整性，如违反了唯一性约束
     */
    @ExceptionHandler(DataIntegrityViolationException.class)
    @ResponseStatus(HttpStatus.OK)
    public RestResponse dataIntegrityViolationException(DataIntegrityViolationException e) {
        e.printStackTrace();
        return ResponseCreator.createFailResponse(e.getCause().getMessage());
    }

    /**
     * 空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(HttpStatus.OK)
    public RestResponse nullPointerException(NullPointerException e) {
        e.printStackTrace();
        return ResponseCreator.createFailResponse("操作失败,存在空指针");
    }

    /**
     * 算数异常
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseStatus(HttpStatus.OK)
    public RestResponse arithmeticException(ArithmeticException e) {
        e.printStackTrace();
        return ResponseCreator.createFailResponse("操作失败,存在异常的运算条件");
    }

    /**
     * 类型转换异常
     */
    @ExceptionHandler(ClassCastException.class)
    @ResponseStatus(HttpStatus.OK)
    public RestResponse classCastException(ClassCastException e) {
        e.printStackTrace();
        return ResponseCreator.createFailResponse("操作失败,存在异常的类型转换");
    }

    /**
     * post请求参数校验异常
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseStatus(HttpStatus.OK)
    public RestResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        BindingResult bindingResult = ex.getBindingResult();
        StringBuilder sb = new StringBuilder("校验失败:");
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            sb.append(fieldError.getDefaultMessage()).append(", ");
        }
        String msg = sb.toString();
        msg = ("".equals(msg)?"":msg.substring(0,msg.length()-1));
        return ResponseCreator.createFailResponse(msg);
    }

    /**
     * get请求参数校验异常
     */
    @ExceptionHandler({ConstraintViolationException.class})
    @ResponseStatus(HttpStatus.OK)
    public RestResponse handleConstraintViolationException(ConstraintViolationException ex) {
        String s = ex.getConstraintViolations()
                .stream()
                .map(ConstraintViolation::getMessage)
                .collect(Collectors.toList()).get(0);
        return ResponseCreator.createFailResponse(s);
    }

    /**
     * EmptyResultDataAccessException
     */
    @ExceptionHandler(EmptyResultDataAccessException.class)
    @ResponseStatus(HttpStatus.OK)
    public RestResponse emptyResultDataAccessException(Exception e) {
        e.printStackTrace();
        return ResponseCreator.createFailResponse("返回了空的数据集合");
    }
    /**
     * Exception异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public RestResponse exception(Exception e) {
        e.printStackTrace();
        return ResponseCreator.createFailResponse(e.getMessage());
    }
}
